package com.futvan.z.framework.util;

import com.futvan.z.erp.crm_customer_ticket.excel_ticket;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class TicketExcelUtil {
    /**
     * 从第一行开始读取excel
     *
     * @param path   excel路径
     * @param aimClass 实体类字节码对象
     * @param <T>  实体类
     * @return 封装excel的内容集合
     */
    public static <T> List<T> parseFromExcel(String path, Class<T> aimClass) {
        return parseFromExcel(path, 0, aimClass);
    }



    /**
     * 从第自定义行开始读取excel
     *
     * @param path   excel路径
     * @param firstIndex   自定义开始有效行
     * @param aimClass 实体类字节码对象
     * @param <T>  实体类
     * @return 封装excel的内容集合
     */
    public static <T> List<T> parseFromExcel(String path, int firstIndex, Class<T> aimClass) {


        List<T> result = new ArrayList<T>();
        try {
            FileInputStream fis = new FileInputStream(path);
            Workbook workbook = WorkbookFactory.create(fis);
            //对excel文档的第一页,即sheet1进行操作
            Sheet sheet = workbook.getSheetAt(0);
            int lastRaw = sheet.getLastRowNum() + 1;
            for (int i = firstIndex; i < lastRaw; i++) {
                //第i行
                Row row = sheet.getRow(i);
                T parseObject = aimClass.newInstance();
                Field[] fields = aimClass.getDeclaredFields();
                for (int j = 0; j < fields.length; j++) {
                    Field field = fields[j];
                    field.setAccessible(true);
                    Class<?> type = field.getType();
                    //第j列
                    Cell cell = row.getCell(j);
                    if (cell == null) {
                        continue;
                    }
                    //很重要的一行代码,如果不加,像12345这样的数字是不会给你转成String的,只会给你转成double,而且会导致cell.getStringCellValue()报错
                    cell.setCellType(CellType.STRING);
                    String cellContent = cell.getStringCellValue();
                    cellContent = "".equals(cellContent) ? "0" : cellContent;
                    if (type.equals(String.class)) {
                        field.set(parseObject, cellContent);
                    } else if (type.equals(char.class) || type.equals(Character.class)) {
                        field.set(parseObject, cellContent.charAt(0));
                    } else if (type.equals(int.class) || type.equals(Integer.class)) {
                        field.set(parseObject, Integer.parseInt(cellContent));
                    } else if (type.equals(long.class) || type.equals(Long.class)) {
                        field.set(parseObject, Long.parseLong(cellContent));
                    } else if (type.equals(float.class) || type.equals(Float.class)) {
                        field.set(parseObject, Float.parseFloat(cellContent));
                    } else if (type.equals(double.class) || type.equals(Double.class)) {
                        field.set(parseObject, Double.parseDouble(cellContent));
                    } else if (type.equals(short.class) || type.equals(Short.class)) {
                        field.set(parseObject, Short.parseShort(cellContent));
                    } else if (type.equals(byte.class) || type.equals(Byte.class)) {
                        field.set(parseObject, Byte.parseByte(cellContent));
                    } else if (type.equals(boolean.class) || type.equals(Boolean.class)) {
                        field.set(parseObject, Boolean.parseBoolean(cellContent));
                    }
                }
                result.add(parseObject);
            }
            fis.close();
            return result;
        } catch (

                Exception e) {
            e.printStackTrace();
        }
        return result;
    }



    /**
     * 带标题写入Excel
     *
     * @param beans  bean集合
     * @param path  写入路径
     * @param <T> 实体类类型
     */
    public static <T> String writeExcelWithTitle(List<T> beans, String path) {
        String s = writeExcel(beans, path, true);
        return s;
    }


    /**
     * 仅把数据写入Excel
     *
     * @param beans  bean集合
     * @param path  写入路径
     * @param <T> 实体类类型
     */
    public static <T> void writeExcel(List<T> beans, String path) {
        writeExcel(beans,path,false);
    }

    private static <T> String writeExcel(List<T> beans, String path, boolean writeTitle) {
        if(beans == null || beans.size() == 0) {
            return "";
        }
        Workbook workbook = new HSSFWorkbook();
        FileOutputStream fos = null;
        int offset = writeTitle ? 1 : 0;
        try {
            Sheet sheet = workbook.createSheet();
            for (int i = 0; i < beans.size() + offset; ++i) {
                if(writeTitle && i == 0) {createTitle(beans, sheet);continue;}
                Row row = sheet.createRow(i);
                T bean = beans.get(i - offset);
                Field[] fields = bean.getClass().getDeclaredFields();
                for (int j = 0; j < fields.length; j++) {
                    Field field = fields[j];
                    field.setAccessible(true);
                    Cell cell = row.createCell(j);
                    //Date,Calender都可以 使用  +"" 操作转成字符串
                    cell.setCellValue(field.get(bean)+"");
                }
            }
            fos = new FileOutputStream(path);
            workbook.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return path;
    }

    private static <T> void createTitle(List<T> beans,Sheet sheet){
        Row row = sheet.createRow(0);
        T bean = beans.get(0);
        Field[] fields = bean.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            field.setAccessible(true);
            Cell cell = row.createCell(i);
            switch (i){
                case 0:
                    cell.setCellValue("客户编号");
                    break;
                case 1:
                    cell.setCellValue("派送券编号");
                    break;
                case 2:
                    cell.setCellValue("数量");
                    break;
                case 3:
                    cell.setCellValue("关联订单号");
                    break;
                case 4:
                    cell.setCellValue("收货地址");
                    break;
                case 5:
                    cell.setCellValue("");
                    break;
            }
        }
    }





    public static void main(String[] args) throws Exception {

        List<excel_ticket> ticketExcel = TicketExcelUtil.parseFromExcel("d:/999999.xls", 1, excel_ticket.class);

        for (excel_ticket t : ticketExcel) {
            System.out.println(t.getTicket());
        }

    }
}
